package com.xcity.game.activity2.platform.jg;

import com.xcity.db.entity.Activity;
import com.xcity.db.entity.Player;
import com.xcity.db.entity.User;
import com.xcity.game.account.AccountChannel;
import com.xcity.game.account.AccountService;
import com.xcity.game.activity2.ActivityImpl;
import com.xcity.game.activity2.platform.IPacketHandler;
import com.xcity.game.activity2.platform.PlatformGift;
import com.xcity.game.activity2.platform.PlatformGiftDispatchService;
import com.xcity.game.common.Cause;
import com.xcity.game.common.IRegistry;
import com.xcity.game.common.MessageConst;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.event.GameEvents;
import com.xcity.game.player.cache.PlayerCacheService;
import com.xcity.pomelo.push.msg.PlatformGiftPush;
import com.xcity.pomelo.request.PlatformGiftInfoRequest;
import com.xcity.pomelo.request.PlatformGiftRequest;
import com.xcity.pomelo.response.HintResponse;
import com.xcity.pomelo.response.PlatformGiftInfoResponse;
import com.xcity.pomelo.response.Response;

import naga.x.App;
import naga.x.event.Event;
import naga.x.event.EventListener;
import naga.x.net.Session;
import naga.x.service.ServiceException;

/**
 * 9G礼包（关注礼包）
 * @author yang.li
 *
 */
public class JGGiftActivity extends ActivityImpl implements IPacketHandler, EventListener {
	
	private IRegistry<AccountChannel, IPacketHandler> registry;
	private PlatformGift gift;

	public JGGiftActivity(Activity entity) {
		super(entity);
		this.registry = App.getApp().getServiceManager().get(PlatformGiftDispatchService.class);
	}

	@Override
	public String getId() {
		return getClass().getName();
	}

	@Override
	public void init() {
		gift = entity.getDataObj(PlatformGift.class);
	}

	@Override
	public void save() {
	}

	@Override
	public void startup() throws ServiceException {
		registry.register(AccountChannel.JG, this);
		App.getApp().getEventManager().register(this);
	}

	@Override
	public void shutdown() throws ServiceException {
		registry.unRegister(AccountChannel.JG);
		App.getApp().getEventManager().unRegister(this);
	}

	@Override
	public boolean update() {
		return true;
	}

	@Override
	public void setDirty() {
		init();
	}
	
	@Override
	public int[] getEventTypes() {
		return new int[] { GameEvents.EVENT_PLAYER_LOADED };
	}

	@Override
	public void handleEvent(Event event) {
		playerLoaded(event.getParameter(0));
	}
	
	private void playerLoaded(Player player) {
		byte state = getState(player, gift);
		if (state == PlatformGift.STATE_CAN_RECEIVE) {
			receive(player, gift);
		}
	}

	@Override
	public void requestGiftInfo(Session session, PlatformGiftInfoRequest req) {
		Player player = session.getClient();
		byte state = getState(player, gift);
		session.send(new PlatformGiftInfoResponse(req.getSerial(), gift, state));
	}

	@Override
	public void requestGift(Session session, PlatformGiftRequest req) {
		Player player = session.getClient();
		byte state = getState(player, gift);
//		if (state == PlatformGift.STATE_CAN_NOT_RECEIVE) {
//			session.send(new HintResponse(req.getSerial(), MessageConst.EXCHANGE_CAN_NOT_RECEIVE));
//			return;
//		}
//		if (state == PlatformGift.STATE_RECEIVED) {
//			session.send(new HintResponse(req.getSerial(), MessageConst.EXCHANGE_RECEIVED));
//			return;
//		}
		if (state != PlatformGift.STATE_CAN_RECEIVE) {
			session.send(new PlatformGiftInfoResponse(req.getSerial(), gift, state));
			return;
		}
		receive(player, gift);
//		session.send(new Response(req.getSerial()));
		session.send(new PlatformGiftInfoResponse(req.getSerial(), gift, PlatformGift.STATE_RECEIVE_SUCC));
	}
	
	public int subscribeCallback(String uid) {
		String cuid = AccountChannel.JG.withPrefix(uid);
		AccountService service = App.getApp().getServiceManager().get(AccountService.class);
		User user = service.find(cuid);
		if (user == null) {
			LOG.info("[9G SUBSCRIBE ERR]UID[{}]CAUSE[USER NOT FOUND]", cuid);
			return -1;
		}
		long playerId = user.getDefaultPlayerId();
		Player player = App.getApp().getServiceManager().get(PlayerCacheService.class).find(playerId);
		if (player == null) {
			LOG.info("[9G SUBSCRIBE ERR]UID[{}]PID[{}]CAUSE[PLAYER NOT FOUND]", cuid, playerId);
			return -1;
		}
		byte state = subscribed(player, gift);
		if (state == PlatformGift.STATE_CAN_RECEIVE) {
			if (player.isOnline()) {
				receive(player, gift);
			}
			return 1;
		}
		return 0;
	}
	
	private byte subscribed(Player player, PlatformGift gift) {
		byte state = getState(player, gift);
		if (state != PlatformGift.STATE_RECEIVED) {
			player.poolSet(getPoolKey(gift), (int) PlatformGift.STATE_CAN_RECEIVE);
			return PlatformGift.STATE_CAN_RECEIVE;
		}
		return state;
	}
	
	private byte getState(Player player, PlatformGift gift) {
		int id = player.getPool().getInt(getPoolKey(gift), PlatformGift.STATE_CAN_NOT_RECEIVE);
		if (id == PlatformGift.STATE_CAN_NOT_RECEIVE) {
			return PlatformGift.STATE_CAN_NOT_RECEIVE;
		}
		if (id == gift.id) {
			return PlatformGift.STATE_RECEIVED;
		}
		return PlatformGift.STATE_CAN_RECEIVE;
	}
	
	private void receive(Player player, PlatformGift gift) {
		player.poolSet(getPoolKey(gift), gift.id);
		gift.content.receive(player, Cause.JG_GIFT, false);
		player.send(new PlatformGiftPush(gift, PlatformGift.STATE_RECEIVED));
	}
	
	private static String getPoolKey(PlatformGift gift) {
		return PlayerPool.PROPERTY_9G_GIFT_PREFIX + gift.type;
	}

}
